import store from '@/store';
import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'


// 把这段代码直接粘贴到router/index.js中的Vue.use(VueRouter)之前,解决重复点击切换同一个路由地址报错的问题
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function(location) {
    return originalPush.call(this, location).catch(err => {})
};

Vue.use(VueRouter)

const routes = [{
        path: '/',
        redirect: Home

    },
    {
        path: '/goods',
        name: 'Goods',
        component: () =>
            import ( /* webpackChunkName: "goods" */ '../views/Goods.vue')
    },
    {
        path: '/order',
        name: 'Order',
        component: () =>
            import ( /* webpackChunkName: "order" */ '../views/Order.vue')
    },
    {
        path: '/user',
        name: 'User',
        component: () =>
            import ( /* webpackChunkName: "User" */ '../views/User.vue'),
        redirect: "/user/cart",
        children: [{
            path: "/user/cart",
            component: () =>
                import ( /* webpackChunkName: "Cart" */ '../components/User/Cart.vue'),
        }]
    },
    {
        path: '/free',
        name: 'Free',
        component: () =>
            import ( /* webpackChunkName: "Free" */ '../views/Free.vue')
    },
    {
        path: '/home',
        name: 'Home',
        component: () =>
            import ( /* webpackChunkName: "Home" */ '../views/Home.vue')
    },
    {
        path: '/details',
        name: 'Details',
        component: () =>
            import ( /* webpackChunkName: "Details" */ '../views/Details.vue')
    }, {
        path: '*',
        name: 'Error',
        component: () =>
            import ( /* webpackChunkName: "Error" */ '../views/Error.vue')
    }

]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})


// // 全局前置导航守卫
// router.beforeEach((to, from, next) => {
//     // 什么时候执行这里的代码：只要发生路由变化，就会执行这里的代码
//     console.log("执行了全局前置导航守卫的代码");
//     console.log("to为:", to); //要去的路由对象
//     console.log("from为:", from); //从哪个路由对象触发

//     //如果去的是/user 需要判断是否已经登录(本地有没有token)
//     if (to.path == "/User") {
//         // 判断是否已经登录
//         let token = localStorage.getItem("x-auth-token");
//         if (token) {
//             // 有登陆就放行
//             next()
//         } else {
//             // 提示还没有登录
//             store._actions["toastStatus/asyncchanisshowToast"][0]({
//                 type: "danger",
//                 msg: "你还没有登录!请先登录"

//             })

//         }
//         return //需要在这里书写return ，防止上面判断有登录之后，连续执行两次next()
//     }
//     next() //next()表示放行通过，没有执行next() 就会跳转不过去
// })










export default router